/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zhou.common.mybatis.template.shiro.api;

import com.google.common.collect.Lists;
import com.zhou.common.mybatis.template.shiro.dto.UserInformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/idm")
public class ApiUsersResource {

    @Autowired
    protected UserDetailsService userDetailsService;

    private static List<UserInformation> list;
    {
        UserInformation userInformation=new UserInformation();
        userInformation.setId("user");
        userInformation.setFirstName("userfirst");
        userInformation.setLastName("userlast");
        userInformation.setFullName("userFullname");
        userInformation.setEmail("test-user@example-domain.tld");
        List<String> privileges=Lists.newArrayList("access-modeler","access-rest-api","access-admin","access-task");
        userInformation.setPrivileges(privileges);

        UserInformation userInformation1=new UserInformation();
        userInformation1.setId("admin");
        userInformation1.setFirstName("adminfirst");
        userInformation1.setLastName("adminlast");
        userInformation1.setFullName("adminFullname");
        userInformation1.setEmail("test-admin@example-domain.tld");
        userInformation1.setPrivileges(privileges);
        list=Lists.newArrayList(userInformation,userInformation1);
    }

    /**
     *         user-id: admin
     *         password: 1
     *         first-name: Test
     *         last-name: Administrator
     *         email: test-admin@example-domain.tld
     * @param userId
     * @return
     */
    @GetMapping(value = "/users/{userId}", produces = {"application/json"})
    public UserInformation getUserInformation(@PathVariable String userId) {
        Map<String, UserInformation> collect = list.stream().collect(Collectors.toMap(t -> t.getId(), t -> t));
        return collect.get(userId);
    }

    @GetMapping(value = "/users", produces = {"application/json"})
    public List<UserInformation> findUsersByFilter(@RequestParam("filter") String filter) {
        return list;
    }

}
